package com.jxt.myjxtmanager.ui.model;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.db.StudentModel;
import com.jxt.myjxtmanager.entity.StudentMsgListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/3/4 0004.
 */
public class StudentMsgMo {

    private String str = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"result\": [\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"studentNum\": \"LQ100022304\",\n" +
            "            \"studentName\": 马原,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-04,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897531\",\n" +
            "            \"urgentPhone\": \"15836599563\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001312,\n" +
            "            \"studentNum\": \"LQ100022305\",\n" +
            "            \"studentName\": 刘伟,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-02,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"13756897531\",\n" +
            "            \"urgentPhone\": \"15836599456\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001313,\n" +
            "            \"studentNum\": \"LQ100022306\",\n" +
            "            \"studentName\": 阿迪斯,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-10,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897895\",\n" +
            "            \"urgentPhone\": \"15836595648\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001314,\n" +
            "            \"studentNum\": \"LQ100022307\",\n" +
            "            \"studentName\": 逗你玩,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2004-05,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18655867531\",\n" +
            "            \"urgentPhone\": \"15836599023\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001351,\n" +
            "            \"studentNum\": \"LQ100022308\",\n" +
            "            \"studentName\": 小明,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2006-02,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897452\",\n" +
            "            \"urgentPhone\": \"15936599036\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001316,\n" +
            "            \"studentNum\": \"LQ100022309\",\n" +
            "            \"studentName\": 小明他哥,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2006-04,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18756845862\",\n" +
            "            \"urgentPhone\": \"15836599563\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001317,\n" +
            "            \"studentNum\": \"LQ100022310\",\n" +
            "            \"studentName\": 大明,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-07,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897531\",\n" +
            "            \"urgentPhone\": \"15836599563\"\n" +
            "        }\n" +
            "        \n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";


    /**
     * 总数据
     */
    private List<Student> queryList;
    private final Context context;
    private OnFindAllDataListener onFindAllDevicesListener;

    private Gson mGson;

    public StudentMsgMo(Context context) {
        this.context = context;
        queryList = new ArrayList<>();
        mGson = new Gson();
    }

    public void setOnFindAllDevicesListener(OnFindAllDataListener onFindAllDevicesListener) {
        this.onFindAllDevicesListener = onFindAllDevicesListener;
    }

    public List<Student> getQueryList() {
        List<Student> students = StudentModel.getInstance().lazyLoadmessage();
        if (students != null && students.size() > 0) {
            return students;
        }
        return queryList;
    }

    public void setQueryList(List<Student> queryList) {
        this.queryList = queryList;
    }

    public void RequestData() {
        loadData();
    }


    private void loadData() {
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            queryList = StudentModel.getInstance().lazyLoadmessage();
            if (queryList != null && queryList.size() > 0) {
            } else {
                StudentMsgListBean mResult = mGson.fromJson(str, StudentMsgListBean.class);
                queryList = mResult.getStudentMsgBeans();
                StudentModel.getInstance().insertOrReplaceAll(queryList);
            }
            onFindAllDevicesListener.onFindSuccess();
        }
    };


    /**
     * 请求监听
     */
    public interface OnFindAllDataListener {
        void onFindSuccess();

        void onFailure();

        void onError();
    }

}
